package cn.kgc.service;

import cn.kgc.config.ExchangeConfirmCallback;
import cn.kgc.config.QueueConfirmCallback;
import cn.kgc.config.RabbitMqConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.PostConstruct;

@Service
@Slf4j
public class ProducerService {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    ExchangeConfirmCallback exchangeConfirmCallback;

    @Autowired
    QueueConfirmCallback queueCallback;


    @PostConstruct
    public void init(){
        rabbitTemplate.setConfirmCallback(exchangeConfirmCallback);
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setReturnCallback(queueCallback);
    }


    public void testSendByTopics(){
        for (int i=0;i<5;i++){
            String message = "发送短信以及邮件信息给到用户："+i;
            CorrelationData correlationData1 = new CorrelationData(""+i);
            correlationData1.setReturnedMessage(new Message(message.getBytes()));
            rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_TOPICS_INFORM,"inxxxform.sms",message,correlationData1);
            System.out.println("Send Message is:'" + message + "'");
        }
    }

}
